<template>
  <div class="app-container">
    <el-form style="box-sizing: border-box;flex-wrap: wrap;" :model="queryParams" ref="queryForm" :inline="true"
      size="small" v-show="showSearch">
      <el-form-item label="选择小区：" prop="plotId">
        <el-select v-model="queryParams.plotId" placeholder="请选择小区" @change="xiaoquChange" clearable>
          <el-option v-for="(item, index) in xiaoquData" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择楼栋：" prop="buildId">
        <el-select v-model="queryParams.buildId" placeholder="请选择楼栋" @change="loudonChange" clearable>
          <el-option v-for="(item, index) in loudongData" :key="index" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择单元：" prop="unitId">
        <el-select v-model="queryParams.unitId" placeholder="请选择单元" clearable>
          <el-option v-for="(item, index) in danyuanData" :key="index" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型：" prop="equipmentType">
        <el-select v-model="queryParams.equipmentType" placeholder="请选择设备类型" clearable>
          <el-option v-for="(item, index) in shebeiData" :key="index" :label="item.dictLabel"
            :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="协议名称" prop="protocolType">
        <el-select v-model="queryParams.protocolType" placeholder="请选择协议" clearable style="width: 170px">
          <el-option v-for="dict in dict.type.protocol_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item prop="dtuName" label="所属dtu：">
        <!-- <el-input v-model="queryParams.dtu" placeholder="请输入所属dtu" clearable @keyup.enter.native="handleQuery" /> -->
        <el-autocomplete id="dtu" v-model="queryParams.dtuName" :fetch-suggestions="querySearchAsync" placeholder="请输入所属dtu" :trigger-on-focus="false" @select="handleSelect"></el-autocomplete>
      </el-form-item>
      <el-form-item prop="equipmentName" label="设备名称：">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item prop="equipmentCode" label="设备编号：">
        <el-input v-model="queryParams.equipmentCode" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设备分组：" prop="equipmentGroupId">
        <el-select v-model="queryParams.equipmentGroupId" placeholder="请选择设备分组" clearable>
          <el-option v-for="(item, index) in dtuGroupSearchList" :key="index" :label="item.groupName"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['equipmentMessage:equipmentMessage:add']">添加设备</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="pilinagHandleCilcik"
          v-hasPermi="['equipmentMessage:equipmentMessage:remove']">批量删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" @click="setGroupByIds"
          v-hasPermi="['equipmentMessage:equipmentMessage:edit']">批量分组</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-upload2" size="mini" @click="importExport"
          v-hasPermi="['equipmentMessage:equipmentMessage:export']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['equipmentMessage:equipmentMessage:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentMessageList" @selection-change="handleSelectionChange">
      <el-table-column v-if="selectFlag" show-overflow-tooltip type="selection" width="55" align="center" />
      <!-- <el-table-column show-overflow-tooltip label="dtu名称" align="center" prop="dtuName" /> -->
      <!-- <el-table-column show-overflow-tooltip label="设备" align="center" prop="equipmentName" /> -->
      <!-- <el-table-column show-overflow-tooltip label="小区id" align="center" prop="lotId" /> -->
      <el-table-column show-overflow-tooltip label="小区" align="center" prop="lotName" />
      <!-- <el-table-column show-overflow-tooltip label="楼栋id" align="center" prop="buildId" /> -->
      <el-table-column show-overflow-tooltip label="楼栋" align="center" prop="buildName" />
      <!-- <el-table-column show-overflow-tooltip label="单元id" align="center" prop="unitId" /> -->
      <el-table-column show-overflow-tooltip label="单元" align="center" prop="unitName" />
      <!-- <el-table-column show-overflow-tooltip label="住户id" align="center" prop="householdId" /> -->
      <el-table-column show-overflow-tooltip label="房号" align="center" prop="houseNumber" />
      <el-table-column show-overflow-tooltip label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status == 0">未知</div>
          <div v-else-if="scope.row.status == 1">在线</div>
          <div v-else-if="scope.row.status == 2">离线</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="所属dtu" align="center" prop="dtuName" />
      <el-table-column show-overflow-tooltip label="设备名称" align="center" prop="equipmentName" />
      <el-table-column show-overflow-tooltip label="设备编号" align="center" prop="equipmentCode" />
      <el-table-column show-overflow-tooltip label="设备类型" align="center" prop="equipmentTypeName" />
      <el-table-column show-overflow-tooltip label="协议类型" align="center" width="100" prop="protocolName" />
      <el-table-column show-overflow-tooltip label="设备分组" align="center" prop="equipmentGroupName" />
      <el-table-column show-overflow-tooltip label="设备地址" align="center" prop="address" />
      <el-table-column show-overflow-tooltip label="上次抄表时间" align="center" prop="updateTime" />
      <!-- <el-table-column show-overflow-tooltip label="是否主动抄表" align="center" prop="autoRead" /> -->
      <!-- <el-table-column show-overflow-tooltip label="备注" align="center" prop="remark" /> -->
      <el-table-column width="100" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['equipmentMessage:equipmentMessage:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['equipmentMessage:equipmentMessage:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="80px"
        style="display: flex; flex-wrap: wrap;height: unset;justify-content: space-between;background-color: unset;padding: 0 20px;">

        <el-form-item label="设备类型" prop="equipmentType" style="margin-bottom: 30px;">
          <el-select v-model="form.equipmentType" placeholder="请选择设备类型">
            <el-option v-for="(item, index) in shebeiDataAdd" :key="index" :label="item.dictLabel"
              :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-if="form.equipmentType != 3" label="dtuId" prop="dtuId" style="margin-bottom: 30px;">
          <el-select v-model="form.dtuId" placeholder="请选择dtuId" clearable>
            <el-option v-for="dict in dtuIdSelectList" :key="dict.id" :label="dict.name" :value="dict.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="协议类型" prop="protocolType" style="margin-bottom: 30px;">
          <el-select :disabled="!form.equipmentType" v-model="form.protocolType" placeholder="请选择协议类型" clearable>
            <el-option v-for="dict in conputedAgreementSelectList" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item prop="lotId" label="小区名称：" style="margin-bottom: 30px;">
          <!-- <el-select v-model="form.plotId" placeholder="请选择小区" clearable @change="xiaoquChangeAdd"> -->
          <el-select v-model="form.lotId" placeholder="请选择小区" clearable @change="xiaoquChangeAdd">
            <el-option v-for="dict in xiaoquDataAdd" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        
        <el-form-item prop="buildId" label="所属楼栋：" style="margin-bottom: 30px;">
          <el-select v-model="form.buildId" placeholder="请选择楼栋" clearable @change="loudonChangeAdd">
            <el-option v-for="dict in loudongDataAdd" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item prop="unit" label="单元号：" style="margin-bottom: 30px;">
          <el-select v-model="form.unitId" placeholder="请选择单元号" clearable @change="danyuanChangeAdd">
            <el-option v-for="dict in danyuanDataAdd" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属住户" prop="householdId" style="margin-bottom: 30px;">
          <el-select v-model="form.householdId" placeholder="请选择住户" clearable>
            <el-option v-for="dict in zhuhuDataAdd" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentName" style="margin-bottom: 30px;">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备编号" prop="equipmentCode" style="margin-bottom: 30px;">
          <el-input v-model="form.equipmentCode" placeholder="请输入设备编号" />
        </el-form-item>

        <el-form-item label="设备分组" prop="equipmentGroupId" style="margin-bottom: 30px;">
          <el-select v-model="form.equipmentGroupId" placeholder="请选择设备分组">
            <el-option v-for="(item, index) in shbeiFenZu" :key="index" :label="item.groupName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备地址" prop="address" style="margin-bottom: 30px;">
          <el-input v-model="form.address" placeholder="请输入设备地址" />
        </el-form-item>
        <el-form-item label="主动抄表" prop="autoRead" style="margin-bottom: 30px;">
          <el-select v-model="form.autoRead" placeholder="请选择设备类型">
            <el-option v-for="(item, index) in chaobiaoData" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="position: unset;width: 100%;">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 批量分组 -->
    <el-dialog title="批量分组" :visible.sync="openGroups" width="450px">
      <el-form ref="formGroups" :model="formGroups" :rules="groupsrules" label-position="left" label-width="80px"
        style="display: flex; flex-wrap: wrap;height: unset;justify-content: space-around;background-color: unset;padding: 0;">


        <!--<el-form-item prop="lotId" label="小区名称：">-->
        <!--<el-select v-model="formGroups.lotId" placeholder="请选择小区" disabled="true"  @change="xiaoquDataGroupsChange()">-->
        <!--<el-option v-for="dict in xiaoquDataGroups" :key="dict.value" :label="dict.label" :value="dict.value" />-->
        <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="设备分组" prop="equipmentGroupId" style="position: unset;margin-right: auto;margin-left: 4.5%;">
          <el-select v-model="formGroups.equipmentGroupId" placeholder="请选择设备分组" clearable>
            <el-option v-for="(item, index) in shbeiFenZuGroups" :key="index" :label="item.groupName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormGroups">确 定</el-button>
        <el-button @click="cancelGroups">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入 -->
    <el-dialog title="设备" :visible.sync="upload.deviceImportShow" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的楼栋数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="downloadTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.deviceImportShow = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listEquipmentMessage, getEquipmentMessage, delEquipmentMessage,
  addEquipmentMessage, updateEquipmentMessage, setGroupByIds, dtuSelectList
} from "@/api/equipmentMessage/equipmentMessage";
// import { equipmentMessageList } from "@/api/mock"
// import data5 from "@/utils/data5.json"
import {
  listCPlotSelect, listBuildSelect, listBuildUnitSelect,
  listEquipmentGroupSelect, listHouseholdSelect, listCustomModelSelect,
  getDtuUsrByName
} from "@/api/system/utils"
import { getDicts } from "@/api/system/dict/data";
import { getToken } from "@/utils/auth";

export default {
  name: "EquipmentMessage",
  dicts: ["protocol_type"],
  data() {
    return {
      udtname:"",
      xiaoquData: [],
      xiaoquDataAdd: [],
      xiaoquDataGroups: [],
      loudongDataAdd: [],
      danyuanDataAdd: [],
      zhuhuDataAdd: [],
      loudongData: [],
      danyuanData: [],
      shebeiData: [],
      shebeiDataAdd: [],
      dtuIdSelectList: [],
      dtuGroupSearchList: [],
      chaobiaoData: [
        { label: '是', value: '1' },
        { label: '否', value: '2' },
      ],
      shbeiFenZu: [],
      shbeiFenZuGroups: [],
      selectFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selects: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      equipmentMessageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openGroups: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentGroupId: null,
        equipmentName: null,
        equipmentCode: null,
        areaId: null,
        longdongId: null,
        danyuanId: null,
        plotId: null,
        buildId: null,
        unitId: null,
        typeId: null,
        type: null,
        dtuName:null,
        equipmentType:null,
        protocolType:null,
      },
      // 表单参数
      form: {},
      formGroups: { equipmentGroupId: null },
      // 表单校验
      rules: {
        protocolType: [
          { required: true, message: "协议类型不能为空", trigger: "blur" }
        ],
        dtuId: [
          { required: true, message: "dtuId不能为空", trigger: "blur" }
        ],
        plotId: [
          { required: true, message: "小区id不能为空", trigger: "blur" }
        ],
        // buildId: [
        //   { required: true, message: "楼栋id不能为空", trigger: "blur" }
        // ],
        equipmentName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        equipmentCode: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        equipmentType: [
          { required: true, message: "设备类型不能为空", trigger: "change" }
        ],
        equipmentGroupId: [
          { required: true, message: "设备分组不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "设备地址不能为空", trigger: "blur" }
        ],
        autoRead: [
          { required: true, message: "是否主动抄表不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
      groupsrules: {
        // lotId: [
        //   { required: true, message: "小区id不能为空", trigger: "blur" }
        // ],
        equipmentGroupId: [
          { required: true, message: "设备分组不能为空", trigger: "blur" }
        ],

      },
      upload: {
        // 是否显示弹出层（用户导入）
        deviceImportShow: false,
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/equipmentMessage/importData"
      },
      agreementSelectList: [],
    };
  },
  computed: {
    conputedAgreementSelectList() {
      if (this.form.equipmentType) {
        let obj = this.shebeiDataAdd.find(item => item.dictValue == this.form.equipmentType)
        return this.agreementSelectList.filter(item => {
          return item.dictLabel.search(obj.dictLabel) != -1 || item.dictValue === '6'
        })
      } else {
        return this.agreementSelectList
      }
    }
  },
  created() {
    listCPlotSelect({}).then(res => {
      this.xiaoquData = res.rows
      this.xiaoquDataAdd = res.rows
      this.xiaoquDataGroups = res.rows
      this.queryParams.plotId = res.rows[0].value
    }).then(res=>{
      if(this.$route.params.plotId){
      this.getRouterData()
       }
       listBuildSelect({ plotId:this.queryParams.plotId}).then((res) => {
          this.loudongData = res.rows;
        });
      this.getList();
    });
    
    this.getDictList()
  },
  mounted(){

  },
  methods: {
    // 获取字典数据 协议类型和设备类型
    getDictList() {
      getDicts("equipment_type").then(res => {
        console.log("equipment_type",res.data);
        this.shebeiData = res.data
        this.shebeiDataAdd = res.data
      });
      getDicts("protocol_type").then(res => {
        console.log("protocol_type",res.data);
        this.agreementSelectList = res.data
      });
    },
    xiaoquChange() {
      listBuildSelect({ plotId: this.queryParams.plotId }).then(res => {
        this.queryParams.buildId = null;
        this.queryParams.unitId = null;
        this.loudongData = res.rows
        this.danyuanData = []
      });
    listEquipmentGroupSelect({plotId: this.queryParams.plotId }).then(res => {
      this.dtuGroupSearchList = res.rows
    })
    },
    xiaoquDataGroupsChange() {
      listEquipmentGroupSelect({ plotId: this.formGroups.plotId }).then(res => {
        this.formGroups.equipmentGroupId = "";
        this.shbeiFenZuGroups = res.rows
      });
    },
    xiaoquChangeAdd(buildIdNull) {
      if (buildIdNull == null) {
        buildIdNull = true
      }
      listBuildSelect({ plotId: this.form.lotId }).then(res => {
        if (buildIdNull) {
          this.form.buildId = null;
          this.form.unitId = null;
        }
        this.loudongDataAdd = res.rows
      });
      listEquipmentGroupSelect({ plotId: this.form.lotId }).then(res => {
        if (buildIdNull) {
          this.form.equipmentGroupId = null;
        }
        this.shbeiFenZu = res.rows
      });
    },
    loudonChange() {
      listBuildUnitSelect({ buildId: this.queryParams.buildId }).then(res => {
        this.queryParams.unitId = null;
        this.danyuanData = res.rows
      });
    },
    loudonChangeAdd(unitIdNull) {
      if (unitIdNull == null) {
        unitIdNull = true
      }
      listBuildUnitSelect({ buildId: this.form.buildId }).then(res => {
        if (unitIdNull) {
          this.form.unitId = null;
        }
        this.danyuanDataAdd = res.rows
      });
    },
    danyuanChangeAdd(setNull) {
      if (setNull == null) {
        setNull = true
      }
      listHouseholdSelect({ unit: this.form.unitId }).then(res => {
        if (setNull) {
          this.form.householdId = null;
        }
        this.zhuhuDataAdd = res.rows
      });
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listEquipmentMessage(this.queryParams).then(response => {
        // equipmentMessageList(this.queryParams).then(response => {
        this.equipmentMessageList = response.rows;
        this.total = response.total;
        this.loading = false;
      }, err => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelGroups() {
      this.openGroups = false;
      this.resetForm("formGroups");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dtuId: null,
        plotId: null,
        buildId: null,
        unitId: null,
        householdId: null,
        equipmentName: null,
        equipmentCode: null,
        equipmentType: null,
        equipmentGroupId: null,
        address: null,
        autoRead: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        equipmentLevel: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.plotId = null
      this.queryParams.protocolType = null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.selects = selection
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getdtuSelectList()
      this.open = true;
      this.title = "添加设备";
      if (this.agreementSelectList.length <= 0 || this.shebeiDataAdd.length <= 0) {
        this.getDictList()
      }
    },
    //dtuid选择列表
    getdtuSelectList() {
      if (this.dtuIdSelectList.length <= 0) {
        dtuSelectList({ pageSize: 999 }).then(res => {
          res.rows = res.rows.map(item => {
            item.id = item.id + ''
            return item
          })
          this.dtuIdSelectList = res.rows
        })
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getdtuSelectList()
      const id = row.id || this.ids
      if (this.agreementSelectList.length <= 0 || this.shebeiDataAdd.length <= 0) {
        this.getDictList()
      }
      getEquipmentMessage(id).then(response => {
        this.form = response.data;
        this.form.buildId == null ? "" : this.form.buildId = this.form.buildId.toString()
        this.form.lotId == null ? "" : this.form.lotId = this.form.lotId.toString()
        this.form.unitId == null ? "" : this.form.unitId = this.form.unitId.toString()
        this.open = true;
        this.title = "修改设备";
        this.xiaoquChangeAdd(false);
        this.loudonChangeAdd(false);
        this.danyuanChangeAdd(false);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.householdId) {
            this.form.equipmentLevel = '3'
          } else if (this.form.unitId) {
            this.form.equipmentLevel = '2'
          } else if (this.form.buildId) {
            this.form.equipmentLevel = '1'
          } else if (this.form.lotId) {
            this.form.equipmentLevel = '0'
          }
          if (this.form.id != null) {
            updateEquipmentMessage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipmentMessage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitFormGroups() {
      this.$refs["formGroups"].validate(valid => {
        if (valid) {
          const ids = this.ids;
          this.formGroups.ids = ids;
          this.formGroups.plotId = this.selects[0].plotId;
          setGroupByIds(this.formGroups).then(response => {
            this.$modal.msgSuccess("保存成功");
            this.openGroups = false;
            this.getList();
          });
        }
      });
    },
    // 批量分组
    setGroupByIds() {
      const ids = this.ids;
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag
      } else {
        const select = this.selects[0];
        var ret = false;
        this.selects.map(item => {
          if (item.plotId != select.plotId) {
            this.$message.error(`只能选一个小区的数据!`);
            ret = true;
          }
        })
        if (ret) {
          return;
        }
        console.log(select, this.formGroups)
        this.formGroups.plotId = select.plotId;
        this.openGroups = true;
        this.formGroups.equipmentGroupId = null;
        this.xiaoquDataGroupsChange();
      }
    },
    // 批量删除
    pilinagHandleCilcik() {
      const ids = this.ids;
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag
      } else {
        this.$modal.confirm('是否删除所选数据？').then(function () {
          return delEquipmentMessage(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      let code = row.equipmentCode
      this.$modal.confirm('是否删除当前数据？').then(function () {
        return delEquipmentMessage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    importExport() {
      this.upload.deviceImportShow = true;
    },
    downloadTemplate() {
      this.download(
        "equipmentMessage/exportTemplate",
        {},
        `设备_${new Date().getTime()}.xlsx`
      );
    },
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.log(response)
      this.upload.deviceImportShow = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
        response.msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('equipmentMessage/exportEquipment', {
        ...this.queryParams
      }, `设备_${new Date().getTime()}.xlsx`)
    },
    // dtu选择事件
    querySearchAsync(queryString, cb) {
      getDtuUsrByName(queryString).then(({rows})=>{
        // console.log(res);
        let results = rows.map(item=>{
         return {
            'value':item.name,
            'id':item.id
          }
        })
        // var results = queryString ? rows.filter(this.createStateFilter(queryString)) : rows;
        cb(results)
      })
      },
      createStateFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
    handleSelect({value}) {
        console.log(value);
        this.queryParams.dtuName = value
      },
    //获取大屏路由参数
    getRouterData(){
      let {name,plotId,data}= this.$route.params
      console.log("大屏参数",this.$route.params);
      if(name){
      this.$set(this.queryParams,"protocolType",data.dicValue)
      this.$set(this.queryParams,"plotId",plotId)
      }
      
    }
  }
};
</script>
<style lang="scss" scoped>

::v-deep .el-autocomplete-suggestion__list{
  color:#fff !important;
}


</style>
